<?php
/**
*  课程
*/
namespace app\api\model;
use think\Model;

class Course extends Model
{
    /**
    *  列表
    */
    public function _list($where = '', $order_by="sort asc", $page_size = PAGE_SIZE_API){
        $obj = $this->order($order_by);
        $data = $this->where($where)
            ->field('*')
            ->order($order_by)
            ->paginate($page_size)
            ->toArray();
        return $data;
    }
    
 
    
    /**
    * 通过id获取信息
    */
    public function _getInfoByID($id){
        return $this->where([['id', '=',$id]])->find();
    }
    
    /**
    * 通过班级id获取所有课程
    */
    public function _getList($id){
        return $this->field('id,title')->where([['class_id', '=', $id]])->select()->toArray();
    }
    
    public function _getListAll($mid, $id){
        return $this->alias('c')
            ->field("c.id, c.title, c.image, IFNULL((select cr.progress from sy_course_record cr where cr.mid = $mid and cr.cid = c.id),0) as progress,IFNULL((select cr.last_cmid from sy_course_record cr where cr.mid = $mid and cr.cid = c.id),0) as last_cmid,IFNULL((select cr.scan_time from sy_course_record cr where cr.mid = $mid and cr.cid = c.id),0) as scan_time, IFNULL((select cm.title  from sy_course_record cr left join sy_course_menu cm on cm.id= cr.last_cmid  where cr.mid = $mid and cr.cid = c.id ),'') as last_title")
            //->where([['c.class_id', '=', $id]])
            ->where([['c.plan_id', '=', $id]])
            ->select()
            ->toArray();
    }

    /**
    * 获取班级课程总时间
    * @param class_id int 班级
    */
    public function _getCourseTimeAll($class_id){
        //获取所有的课程
        $allCourse = $this->field('id')->where(['class_id'=>$class_id])->select();
        $allIds = [];
        foreach($allCourse as $k=>$v){
            $allIds[$k] = $v['id'];
        }
        
        $model = new CourseMenu;
        $alltime = $model->_getCourseTimeAll([["cid", 'in', $allIds]]);
        return $alltime;
    }
}